﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace Star_Wars
{
    public static class SoundManager
    {
        private static List<SoundEffect> explosions = new List<SoundEffect>();
        private static List<Song> songs = new List<Song>();
        private static List<float> durations = new List<float>();

        private static int explosionCount = 4;
        private static int songsCount = 3;

        private static float trackTimer = 0f;
        private static int current = 2;

        //private static SoundEffect mainTheme;
        private static Song mainTheme; 

        private static Random rand = new Random();

        public static void Initialize(ContentManager content)
        {
            try
            {
                mainTheme = content.Load<Song>(@"Sounds\Apache");
                
                for (int i = 1; i <= explosionCount; i++)
                {
                    explosions.Add(content.Load<SoundEffect>(@"Sounds\Explosion" + i.ToString()));
                    //Sou
                }

                for (int i = 1; i <= songsCount; i++)
                {
                    songs.Add(content.Load<Song>(@"Sounds\Theme" + i.ToString()));
                }
                MediaPlayer.Play(songs[current]);
            }
            catch
            {
                Debug.Write("SoundManager Initialization Failed");
            }
        }
        public static void PlayExplosion()
        {
            try
            {
                explosions[rand.Next(0, explosionCount)].Play();
            }
            catch
            {
                Debug.Write("PlayExplosion Failed");
            }
        }
        public static void PlayMainTheme()
        {
            try
            {

                MediaPlayer.Play(mainTheme);
            }
            catch
            {
                Debug.Write("PlayMainTheme Failed");
            }
        }
        public static void Update(GameTime gameTime)
        {
            trackTimer += (float)gameTime.ElapsedGameTime.TotalSeconds;
            if (trackTimer >= songs[current].Duration.TotalSeconds)
            {
                MediaPlayer.Stop();
                int newTrack = current;
                //songs[current].Duration.Seconds
                while (newTrack == current)
                {
                    current = rand.Next(0, 3);
                }
                MediaPlayer.Play(songs[current]);
                trackTimer = 0f;
            }
        }
    }
}
